package com.meihua.baoleyi.utils;

import android.text.TextUtils;

/**
 * Created by liming on 2017/10/16.
 */
public class StringUtils {
    private static StringUtils util;

    private StringUtils() {
    }

    public static StringUtils getInstance() {
        if (util == null) {
            util = new StringUtils();
        }
        return util;
    }

    public static boolean isStringNull(String str) {
        return TextUtils.isEmpty(str) || "null".equals(str) || str == null;
    }

    public static String parseStr(String str) {
        if (isStringNull(str)) {
            return "";
        }
        return str;
    }

    //如果全是数字返回true
    public static boolean isAllNum(String numOrStr) {
        if (numOrStr == null) {
            return false;
        }
        boolean isNumeric = true;//如果全是数字返回true
        for (int i = 0; i < numOrStr.length(); i++) {
            if (!Character.isDigit(numOrStr.charAt(i))) {
                isNumeric = false;
                break;
            }
        }
        return isNumeric;
    }

    //如果全是字母返回true
    public static boolean isAllCode(String numOrStr) {
        boolean isCodeeric = true;//如果全是字母返回true
        for (int i = 0; i < numOrStr.length(); i++) {
            if (!Character.isDigit(numOrStr.charAt(i))) {
                isCodeeric = false;
                break;
            }
        }
        return isCodeeric;
    }

    //不能全是相同的数字或者字母（如：000000、111111、aaaaaa） 全部相同返回true
    public static boolean equalStr(String numOrStr) {
        boolean flag = true;
        char str = numOrStr.charAt(0);
        for (int i = 0; i < numOrStr.length(); i++) {
            if (str != numOrStr.charAt(i)) {
                flag = false;
                break;
            }
        }
        return flag;
    }

    //不能是连续的数字--递增（如：123456、12345678）连续数字返回true
    public static boolean isOrderNumeric(String numOrStr) {
        boolean flag = true;//如果全是连续数字返回true
        boolean isNumeric = true;//如果全是数字返回true
        for (int i = 0; i < numOrStr.length(); i++) {
            if (!Character.isDigit(numOrStr.charAt(i))) {
                isNumeric = false;
                break;
            }
        }
        if (isNumeric) {//如果全是数字则执行是否连续数字判断
            for (int i = 0; i < numOrStr.length(); i++) {
                if (i > 0) {//判断如123456
                    int num = Integer.parseInt(numOrStr.charAt(i) + "");
                    int num_ = Integer.parseInt(numOrStr.charAt(i - 1) + "") + 1;
                    if (num != num_) {
                        flag = false;
                        break;
                    }
                }
            }
        } else {
            flag = false;
        }
        return flag;
    }

    //不能是连续的数字--递减（如：987654、876543）连续数字返回true
    public static boolean isOrderNumeric_(String numOrStr) {
        boolean flag = true;//如果全是连续数字返回true
        boolean isNumeric = true;//如果全是数字返回true
        for (int i = 0; i < numOrStr.length(); i++) {
            if (!Character.isDigit(numOrStr.charAt(i))) {
                isNumeric = false;
                break;
            }
        }
        if (isNumeric) {//如果全是数字则执行是否连续数字判断
            for (int i = 0; i < numOrStr.length(); i++) {
                if (i > 0) {//判断如654321
                    int num = Integer.parseInt(numOrStr.charAt(i) + "");
                    int num_ = Integer.parseInt(numOrStr.charAt(i - 1) + "") - 1;
                    if (num != num_) {
                        flag = false;
                        break;
                    }
                }
            }
        } else {
            flag = false;
        }
        return flag;
    }
    /**
     * Helper function for making null strings safe for comparisons, etc.
     *
     * @return (s == null) ? "" : s;
     */
    public static String makeSafe(String s) {
        return (s == null) ? "" : s;
    }
    /**
     * @param str
     * @return
     */
    public static final String EMPTY = "";
    public static String trim(String str) {
        return str == null ? EMPTY : str.trim();
    }

    /**
     * 截取银行卡号后4位
     * @param bandNumber
     * @return
     */
    public static final String bankLast (String bandNumber) {
        String lastNumber = "";
        if (bandNumber.length() > 10) {
            lastNumber = bandNumber.substring(bandNumber.length() - 4, bandNumber.length());
        }
        return lastNumber;
    }

    /**
     * 隐藏银行卡后6位
     *
     * @return
     */
    public static final String hintIdLast(String id) {
        String substring = "";
        if (id.length() == 18) {
            substring = id.substring(0, id.length() - 6);
            return substring + "******";
        }
        return substring;
    }
}

